/*************************************************************************************
* 	 Copyright (C) 2010 by Information Systems Group, Saarland University  			*
*    http://infosys.cs.uni-saarland.de												*
* 	 																				*
* 	 This file is part of Hadoop++.												 	*
*																					*
*    Hadoop++ is free software: you can redistribute it and/or modify				*
*    it under the terms of the GNU Lesser General Public License as published by	*
*    the Free Software Foundation, either version 3 of the License, or				*
*    (at your option) any later version.											*
*																					*
*    Hadoop++ is distributed in the hope that it will be useful,					*
*    but WITHOUT ANY WARRANTY; without even the implied warranty of					*
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the					*
*    GNU Lesser General Public License for more details.							*
*																					*
*    You should have received a copy of the GNU Lesser General Public License		*
*    along with Hadoop++.  If not, see <http://www.gnu.org/licenses/>.				*
*************************************************************************************/
package unisb.cs.data.types;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import unisb.cs.core.binary.utils.BinaryUtils;

/**
 * This class defines the different data types in Hadoop++
 * We map data types to integer identifiers for better performance.
 *
 */
public class DataTypes {

	/** int, float, char, and date data types */
	public final static int INT_TYPE = 1;
	public final static int FLOAT_TYPE = 2;
	public final static int CHAR_TYPE = 3;
	public final static int DATE_TYPE = 4;
	
	/** The format for date data type */
	public final static DateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd");
	
	
	public static byte[] getSmallestValue(int type){
		switch(type){
		case DataTypes.INT_TYPE:	return BinaryUtils.toBytes(Integer.MIN_VALUE);
		case DataTypes.FLOAT_TYPE:	return BinaryUtils.toBytes(Float.MIN_VALUE);
		case DataTypes.CHAR_TYPE:	return BinaryUtils.toBytes("");
		case DataTypes.DATE_TYPE:	return BinaryUtils.toBytes("01-01-0001");
		}
		return null;
	}
	
	/**
	 * Compare data types
	 * @param val1 First Key
	 * @param val2 Second key
	 * @param type Group key data type
	 * @return 1 if val1 > val2, -1 if val1 < val2, 0 otherwise 
	 */
	public static int compare(byte[] val1, byte[] val2, int type) {
		switch (type) {
		case DataTypes.INT_TYPE:
			int cInt = (BinaryUtils.getInt(val1, 0) - BinaryUtils.getInt(val2, 0));
			if (cInt > 0)
				return 1;
			else if (cInt < 0)
				return -1;
			else
				return 0;
		case DataTypes.FLOAT_TYPE:
			float cFloat = BinaryUtils.getFloat(val1, 0) - BinaryUtils.getFloat(val2, 0);
			if (cFloat > 0)
				return 1;
			else if (cFloat < 0)
				return -1;
			else
				return 0;
		case DataTypes.CHAR_TYPE:
			return (new String(val1).compareTo(new String(val2)));
		case DataTypes.DATE_TYPE:
			return (BinaryUtils.getDate(val1, 0, val1.length).compareTo(BinaryUtils.getDate(val2, 0, val2.length)));
		}
		return 0;
	}
}
